<template>
	<view class="wap">
		<navbar :parameter='parameter'></navbar>
		<view class="box form">
			<view class="line acea-row">
				<view class="tit"> 标题 </view>
				<input type="text" v-model="list.title" placeholder="请输入"
					placeholder-class="color: rgba(0, 0, 0, 0.369);">
			</view>
			<view class="line">
				<view class="tit">上传图片</view>
				<view class="upload-item">
					<view class="upload-box">
						<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple
							:maxCount="4" width="154rpx" height="154rpx">
							<image src="../../static/images/upload.png" mode="widthFix" style="width: 154rpx;"></image>
						</u-upload>
					</view>
				</view>
			</view>
			<view class="line acea-row">
				<view class="tit"> 内容 </view>
				<textarea value="" v-model="list.content" placeholder="请输入详细内容 "placeholder-class="color: rgba(0, 0, 0, 0.369);" />
			</view>

		</view>
		<view class="btn-box">
			<view class="btn" @click="submit">提交</view>
		</view>
		<!-- 提示 -->
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				parameter: {
					'navbar': '1',
					'return': '1',
					'title': '陪诊故事',
					'color': true,
					'class': 'on'
				},
				fileList1: [],
				list: {
					openid:uni.getStorageSync('openid'),
					title: '',
					content: '',
				}

			};
		},
		onLoad() {

		},
		onShow() {

		},

		methods: {
			async addStory() {
				const res = await this.$myRequest({
					url: '/api/Manage/addStory',
					data: this.list
				})
				if(res.data.status == 200){
					this.$refs.uToast.show({
						type: 'success',
						message: '提交成功',
						complete() {
							uni.navigateBack()
						}
					})
				}
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					console.log(JSON.parse(result).data)
					const img = JSON.parse(result).data
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: img
					}))
					fileListLen++
				}

				console.log(event.name, this[`fileList${event.name}`])
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: 'https://dsby.hearoommed.com/api/uploads/uploadImage', // 仅为示例，非真实的接口地址
						// url: 'http://192.168.0.3:11122/api/uploads/uploadImage', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'image',
						formData: {
							openid: uni.getStorageSync('openid')
						},
						success: (res) => {
							console.log('11111')
							setTimeout(() => {
								resolve(res.data)
							}, 1000)
						},
						fail(res) {
							console.log('fail', res)
						}
					});
				})
			},
			submit(){
				var imgArr = [];
				this.fileList1.forEach(function(item) {
					imgArr.push(item.url)
				});
				this.list.images = imgArr.toString();
				this.addStory()
			}

		}
	}
</script>

<style lang="scss" scoped>
	.wap {
		padding: 20rpx;
	}

	.box {
		width: 100%;
		padding: 0 20rpx;
		background-color: #fff;
		border-radius: 20rpx;
		padding-bottom: 50rpx;
	}

	.form .line {
		font-size: 28rpx;
		line-height: 100rpx;
		border-bottom: 1px solid #D9D9DA;

	}

	.form .line:last-of-type {
		border-bottom: none;
	}

	.form .line .tit {
		color: #525252;
		width: 200rpx;
	}

	.form .line input {
		color: #000;
		flex: 1;
		height: 100rpx;
		line-height: 100rpx;
		text-align: right;
	}

	.form .line textarea {
		width: 100%;
		background: #F6F7F8;
		border-radius: 12rpx;
		padding: 30rpx;
		font-size: 26rpx;
		height: 260rpx;
		line-height: 1.4;
	}

	.upload-box {
		padding-bottom: 20rpx;
	}

	.btn-box {
		padding-top: 80rpx;
	}

	.btn-box .btn {
		width: 100%;
		height: 80rpx;
		border-radius: 10rpx;
		line-height: 80rpx;
		text-align: center;
		background-color: #347DFE;
		color: #fff;
		font-size: 32rpx;
	}
</style>
